﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace TransportLayer.NodeToolsLibrary
{
    public class StringTokenizer : IEnumerable, IEnumerator
    {
        string[] tokensizedStrs;
        int index = -1;
        
        public int Count { get { return tokensizedStrs.Length; } }
        
        public StringTokenizer(string target, char[] tokens)
        {
            tokensizedStrs = target.Split(tokens);
        }

        public StringTokenizer()
        {
            tokensizedStrs = new string[0];
        }
        public StringTokenizer(string target, char tokens)
        {
            tokensizedStrs = target.Split(tokens);
        }

        public IEnumerator GetEnumerator()
        {
            return this;
        }
        object IEnumerator.Current
        {
            get
            {
                return tokensizedStrs[index];
            }
        }
        bool IEnumerator.MoveNext()
        {
            if (++index >= tokensizedStrs.Length)
                return false;
            else
                return true;
        }

        public bool MoveNext()
        {
            index++;
            if (++index >= tokensizedStrs.Length)
                return false;
            else
                return true;
        }

        public bool MovePrevious()
        {
            index--;
              if(index < 0)
                  return false;
              else return true;

        }

        public string Current
        {
            get
            {
                try
                {
                    return tokensizedStrs[index];
                }
                catch (IndexOutOfRangeException)
                { return null; }
        }
        }


        void IEnumerator.Reset()
        {
            index = -1;
        }

        public string Last { get { index = tokensizedStrs.Length - 1; if (index > 0) return tokensizedStrs[index]; else return null; } }
        public string First { get { index = 0; return tokensizedStrs[index]; } }

        public void End() { index = tokensizedStrs.Length; }

        public void Add(string str)
        {
            string [] tmp = new string[tokensizedStrs.Length+1];
            for (int i = 0; i < tokensizedStrs.Length; i++)
                tmp[i] = tokensizedStrs[i];

                    tmp[tokensizedStrs.Length]=str;

                    tokensizedStrs = tmp; 
        }
        public int GetLenght() { return tokensizedStrs.Length; }


        public bool MoveTo(int target) 
        {
            if (target < tokensizedStrs.Length)
            {
                index = target;
                return true;
            }
            else
                return false;
        
        }
    }		
}
